from typing import Optional
from datetime import datetime, timezone
from sqlmodel import SQLModel, Field


class Server(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    host: str
    port: int = 22
    username: str
    # Store encrypted password (Fernet). Empty if using key-based auth in future.
    password_enc: Optional[str] = None
    # Optional description or tags
    description: Optional[str] = None


class SessionLog(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    server_id: int = Field(foreign_key="server.id")
    # 使用带时区的 UTC 时间，避免前端解析为本地时间导致偏差
    started_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    ended_at: Optional[datetime] = None
    bytes_count: int = 0
    content: str = ""


class Setting(SQLModel, table=True):
    key: str = Field(primary_key=True)
    value: str

